package com.example.service;

import com.example.dao.CartsDao;
import com.example.entity.Carts;
import com.example.entity.Params;
import com.example.exception.CustomException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CartsService {

    @Resource
    private CartsDao cartsDao;

    public List<Carts> findAll() {
        return cartsDao.selectAll();
    }

    public PageInfo<Carts> findBySearch(Params params) {
        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        List<Carts> list = cartsDao.findBySearch(params);
        return PageInfo.of(list);
    }

    public void add(Carts cart) {
        if(cart.getUserId() == null || cart.getProductId() == null) {
            throw new CustomException("用户ID或商品ID不能为空");
        }
        cartsDao.insertSelective(cart);
    }

    public void update(Carts cart) {
        cartsDao.updateByPrimaryKeySelective(cart);
    }

    public void delete(Integer cartId) {
        cartsDao.deleteByPrimaryKey(cartId);
    }

    public Carts findById(Integer cartId) {
        return cartsDao.selectByPrimaryKey(cartId);
    }
}
